<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		data() {
			return buildFormData({
				yearList:[],
				monthList:[],
				quarterList:[],
				currencyDictCacheKey:'obj-ss-taslesForecastAnalusis-currencyDict-cache',
				indexCacheKey:'obj-ss-taslesForecastAnalusis-index-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			getWorkParam(){
				let _that = this
				return {
					indexId:_that.$refs.autoform.formdata.indexId || ''
				}
			},
			onSelectEndDate(val){
				let _that = this
				if(_that.$refs.autoform.formdata.startDate){
					if(moment(val).isBefore(_that.$refs.autoform.formdata.startDate)){
						let title1 = _that.$refs.autoform.getEditorTitle("title_basic","startDate")
						let title2 = _that.$refs.autoform.getEditorTitle("title_basic","endDate")
						errorModal(_that,'提示信息',title2+'不能小于'+title1,()=>{
							_that.$refs.autoform.clear("title_basic","endDate")
						})
					}
				}
			},
			onSelectPeriod(item){
				let _that = this
				if(item.code == "38549"){
					_that.$refs.autoform.changeEditor("title_basic","year",'data',configSelectData(_that.yearList,'value','name'))
					_that.$refs.autoform.changeEditor("title_basic","monthCode",'data',configSelectData(_that.monthList,'code','dicname'))
					_that.$refs.autoform.changeEditor("title_basic","year",'type',"selectbox")
					_that.$refs.autoform.changeEditor("title_basic",'quarterCode','type',"labelbox")
					_that.$refs.autoform.changeEditor("title_basic",'monthCode','type',"selectbox")
				}else if(item.code == '38550'){
					_that.$refs.autoform.changeEditor("title_basic","year",'data',configSelectData(_that.yearList,'value','name'))
					_that.$refs.autoform.changeEditor("title_basic","quarterCode",'data',configSelectData(_that.quarterList,'code','dicname'))
					_that.$refs.autoform.changeEditor("title_basic","year",'type',"selectbox")
					_that.$refs.autoform.changeEditor("title_basic",'quarterCode','type',"selectbox")
					_that.$refs.autoform.changeEditor("title_basic",'monthCode','type',"labelbox")
				}else{
					_that.$refs.autoform.changeEditor("title_basic","year",'data',configSelectData(_that.yearList,'value','name'))
					_that.$refs.autoform.changeEditor("title_basic","year",'type',"selectbox")
					_that.$refs.autoform.changeEditor("title_basic",'quarterCode','type',"labelbox")
					_that.$refs.autoform.changeEditor("title_basic",'monthCode','type',"labelbox")
				}
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'tsalesForecastAnalysis/getBasicInfo'
					},
					{
						url:'dictZero/getListByCode',
						param:{
							code:38548
						}
					},
					{
						url:'currencyDict/getLocalCurrency'
					},
					{
						url:'annualReportPeriod/getYearList'
					},
					{
						url:'dictZero/getListByCode',
						param:{
							code:'35803'
						}
					},{
						url:'dictZero/getListByCode',
						param:{
							code:'33055'
						}
					}
					
				]
				if(fileId){
					ajaxArr.push(
						{
							url:'tsalesForecastAnalysis/getInfo',
							param:{
								id:fileId
							}
						}
					)
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],'id','posName'),mainData)
					changeEditor("title_basic","periodCode","data",configSelectData(res[2],'code','dicname'),mainData)
					_that.yearList = res[4]
					_that.monthList = res[5]
					_that.quarterList = res[6]
					changeEditor("title_basic","currencyDictId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({}),
							url:'currencyDict/getCurrency',
							initload:true,
							namefield:'currencyName',
							extrafield:'currencyCode',
							notefield:'currencyUnit',
							cachename:_that.currencyDictCacheKey
						}
					},null,mainData)
					changeEditor("title_basic","currencyDictId",{
						defaultValue:res[3].value.id,
						defaultName:res[3].value.currencyCode+'/'+ res[3].value.currencyName+'/'+res[3].value.currencyUnit
					},null,mainData)
					changeEditor("title_basic","indexId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({}),
							url:'indexDict/findSalesForecastAnalysisIndexPage',
							initload:true,
							namefield:'dicname',
							extrafield:'indexUnitName',
							notefield:'logicRelationshipName',
							cachename:_that.indexCacheKey
						}
					},null,mainData)
					if(_that.subEditorObj['title_data']){
						let attr = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['title_data']
						if (hasEditor('title_data', 'areaId', configData)) {
							let title = getEditorTitle('title_data', 'areaId', configData)
							attr.titleTemplate = title+':<%= areaName %>'
						}
						if (hasEditor('title_data', 'placeId', configData)) {
							let title = getEditorTitle('title_data', 'placeId', configData)
							attr.subtitle1Template = title+':<%= placeName %>'
						}
						if (hasEditor('title_data', 'customerShortName', configData)) {
							let title = getEditorTitle('title_data', 'customerShortName', configData)
							attr.subtitle2Template = title+':<%= customerShortName %>'
						}
						changeEditor("title_data",'dataStr',attr,mainData)
					}
					if(fileId){
						let fielArr = getFormFields(mainData)
						fielArr.push('id')
						for(let index in res[7].value.tsalesForecastAnalysis){
							if(fielArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[7].value.tsalesForecastAnalysis[index])
								_that.$set(_that.formdata,index,res[7].value.tsalesForecastAnalysis[index])
							}
						}
						let dataList = _that._.cloneDeep(res[7].value.dataList)
						_that.subEditorObj['title_data'].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'dataStr',JSON.stringify(dataList))
						_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList))
					}
					_that.$nextTick(function(){
						if(!_that.edit){
							_that.$refs.autoform.formdata.userName = res[1].value.userName
							_that.$refs.autoform.formdata.posName = res[1].value.posName
							_that.$refs.autoform.formdata.parentUserName = res[1].value.parentUserName
							_that.$refs.autoform.formdata.instName = res[1].value.instName
							_that.$refs.autoform.formdata.instMainUserName = res[1].value.instMainUserName
						}
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo",'zposId')
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zpsoId")
						}
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				_that.om.formSave('tsalesForecastAnalysis/addTsalesForecastAnalysis',_that.formdata,_that)
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.currencyDictCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							 formdata.currencyDictId = d.id
							if(!d.currencyUnit){
								formdata.currencyDictName = d.currencyCode+'/'+ d.currencyName
							}else{
								formdata.currencyDictName = d.currencyCode+'/'+ d.currencyName+'/'+d.currencyUnit
							}
							uni.removeStorage({
								key:_that.currencyDictCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.indexCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexName = d.dicname
							formdata.indexId = d.id
							uni.removeStorage({
								key:_that.indexCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       